const express = require("express");
const app = express();
// 获取⽂件路径
const resolve = (dir) => require("path").resolve(__dirname, dir);
// 第 1 步：开放dist/client⽬录，关闭默认下载index⻚的选项，不然到不了后⾯路由
app.use(express.static(resolve("../dist/client"), { index: false }));
// 第 2 步：获得⼀个createBundleRenderer
const { createBundleRenderer } = require("vue-server-renderer");
// 第 3 步：服务端打包⽂件地址
const bundle = resolve("../dist/server/vue-ssr-server-bundle.json");
// 第 4 步：创建渲染器
const renderer = createBundleRenderer(bundle, {
  runInNewContext: false, // https://ssr.vuejs.org/zh/api/#runinnewcontext
  template: require("fs").readFileSync(resolve("../public/index.html"), "utf8"), // 宿主⽂件
  clientManifest: require(resolve(
    "../dist/client/vue-ssr-client-manifest.json"
  )), // 客户端清单
});
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

/* 模拟window对象逻辑 */
const resourceLoader = new jsdom.ResourceLoader({
  userAgent:
    "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
}); // 设置UA
const dom = new JSDOM("", {
  url: "https://app.nihaoshijie.com.cn/index.html",
  resources: resourceLoader,
});

global.window = dom.window;
global.document = window.document;
global.navigator = window.navigator;
window.nodeis = true; //给window标识出node环境的标志位

app.get("*", async (req, res) => {
  // 设置url和title两个重要参数
  const context = {
    title: "ssr test",
    url: req.url,
  };
  const html = await renderer.renderToString(context);
  res.send(html);
});
app.listen(4000, () => {
  console.log("server start 4000");
});
